﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinLogicUniversity
{
    public partial class frmPurchaseList : Form
    {
        
        decimal Itemprice, total,totalamount,itotal;
        int Itemqty,totalqty;
        string id;
        public frmPurchaseList()
        {
            InitializeComponent();
        }

        private void frmPurchaseList_Load(object sender, EventArgs e)
        {   
            lblDate.Text = DateTime.Today.ToShortDateString();
            lblDate2.Text = DateTime.Today.ToShortDateString();

            cbSName.DisplayMember = "SName";
            cbSName.ValueMember = "SID";
            cbSName.DataSource = PurchaseManager.getAllSupplier();


            cbCat.DisplayMember = "cName";
            cbCat.ValueMember = "cID";
            cbCat.DataSource = PurchaseManager.getAllCategory();

            Itemprice = PurchaseManager.getItemPrice("I001");
            lblPrice.Text = Itemprice.ToString();
            
            dgPurchaseList2.Columns.Add("iNo", "Item No");
            dgPurchaseList2.Columns.Add("sNo", "Supplier Name");
            dgPurchaseList2.Columns.Add("qty", "Qty");
            dgPurchaseList2.Columns.Add("Amount", "Amount");

           
            List<PurchaseObj> list = new List<PurchaseObj>();
            list=PurchaseManager.getPurchaseList();
            for (int i = 0; i < list.Count; i++)
            {
                try
                {
                    string sup = list[i].SupplierName;
                    //list[i].SupplierName = PurchaseManager.getSupplierName(sup);
                    LogicUniversityT2Entities logic =new LogicUniversityT2Entities();
                    var supName =(from supp in logic.Suppliers where supp.SupplierID == sup select new { iID =supp.SupplierID, iName = supp.SupplierName });
                    foreach (var name in supName)
                    {
                        list[i].SupplierName = name.iName;
                    }
                    this.dgPurchaseList.Rows.Add(list[i].ItemNo, list[i].ItemDescription, list[i].SupplierName, list[i].Qty, list[i].Price, list[i].Amount);
                    }
                catch (IndexOutOfRangeException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            lblTotalAmount.Text =PurchaseManager.getTotalAmount().ToString();
            var slist = PurchaseManager.getSupplier();
            cboSupplierName.DataSource = slist;
            cboSupplierName.DisplayMember = "sName";
        }

        private void btnProceed_Click(object sender, EventArgs e)
        {
            List<PurchaseObj> list = new List<PurchaseObj>();
            foreach (DataGridViewRow row in dgPurchaseList.Rows)
                {
                    if (row != null)
                    {
                        PurchaseObj purchase = new PurchaseObj();
                        purchase.Date=Convert.ToDateTime(lblDate.Text);
                        purchase.ItemNo = row.Cells[0].Value.ToString();
                        purchase.SupplierName = row.Cells[2].Value.ToString();
                        purchase.Qty= Convert.ToInt32(row.Cells[3].Value.ToString());
                        purchase.Amount = Convert.ToDouble(row.Cells[5].Value.ToString());
                        purchase.Total = Convert.ToDouble(lblTotalAmount.Text);
                        list.Add(purchase);
                    }
                 }
            PurchaseManager.addPurchaseList(list);
            frmPurchaseList form = new frmPurchaseList();
            form.Hide();
            frmPurchaseOrder form2 = new frmPurchaseOrder();
            form2.Show();

                }
     
        public List<Object[]> getGridData()
        {
            String itemNo = "";
            String des = "";
            String supName = "";
            String qty = "";
            String price = "";
            String amount = "";
            List<Object[]> data = new List<Object[]>();
            foreach (DataGridViewRow row in dgPurchaseList.Rows)
            {
                if (row != null)
                {
                    itemNo = row.Cells[0].Value.ToString();
                    des = row.Cells[1].Value.ToString();
                    supName = row.Cells[2].Value.ToString();
                    qty = row.Cells[3].Value.ToString();
                    price = row.Cells[3].Value.ToString();
                    amount = row.Cells[4].Value.ToString();
                    data.Add(new object[] { itemNo, des,supName, qty, price, amount });
                    return data;
                }
                else
                {
                    MessageBox.Show("No data in gridview", "DataError");
                    return null;
                }
            }
            return data;
        }

        private void dgPurchaseList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            cboSupplierName.Text = dgPurchaseList.CurrentRow.Cells[2].Value.ToString();
            txtQty.Text = dgPurchaseList.CurrentRow.Cells[3].Value.ToString();
            errorProvider1.Clear();
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            Boolean myvalidate;
            myvalidate = MyValidation();

            try
            {
                if (myvalidate == true)
                {
                    dgPurchaseList.CurrentRow.Cells[2].Value = cboSupplierName.Text;
                    dgPurchaseList.CurrentRow.Cells[3].Value = txtQty.Text;
                }
            }
            catch (SqlException ex)
            {
                MyMessageBox.ShowBox("SQL Error " + ex.Number + ":" + ex.Message);
            }
            
            
        }

        private void cbCat_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cid = cbCat.SelectedValue.ToString();
            cboItem.DataSource = PurchaseManager.getAllItem(cid);
            cboItem.DisplayMember = "iName";
            cboItem.ValueMember = "iID";
        }

        private void btnEdit2_Click(object sender, EventArgs e)
        {
            Boolean myvalidate;
            myvalidate = MyValidate();
            try
            {
                if (myvalidate == true)
                {
                    id = cboItem.SelectedValue.ToString();
                    dgPurchaseList2.CurrentRow.Cells[0].Value = id;
                    dgPurchaseList2.CurrentRow.Cells[1].Value = cbSName.Text;
                    dgPurchaseList2.CurrentRow.Cells[2].Value = txtQty2.Text;
                    dgPurchaseList2.CurrentRow.Cells[3].Value = lblTotal2.Text;
                }
            }
            catch (SqlException ex)
            {
                MyMessageBox.ShowBox("SQL Error " + ex.Number + ":" + ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Boolean myvalidate;
            myvalidate = MyValidate();

            try
            {
                if (myvalidate == true)
                {
                    string qty = txtQty2.Text;
                    Itemqty = Convert.ToInt32(qty);

                    PurchaseObj po = new PurchaseObj();
                    string dummyid = "dummy";
                    id = cboItem.SelectedValue.ToString();
                    if (dummyid != id)
                    {
                        totalqty = Itemqty;
                        po.ItemNo = id;
                        po.SupplierName = cboSupplierName.Text;
                        po.Qty = Convert.ToInt32(totalqty);
                        Itemprice = PurchaseManager.getItemPrice(id);
                        itotal = Itemprice * totalqty;
                        lblAmount.Text = itotal.ToString();
                        po.Amount = Convert.ToDouble(itotal);
                    }
                    else
                    {
                        totalqty += Itemqty;
                        po.Qty = Convert.ToInt32(totalqty);
                        Itemprice = PurchaseManager.getItemPrice(id);
                        itotal = Itemprice * totalqty;
                    }

                    List<PurchaseObj> list = new List<PurchaseObj>();
                    list.Add(po);
                    for (int i = 0; i < list.Count; i++)
                    {
                        this.dgPurchaseList2.Rows.Add(list[i].ItemNo, list[i].SupplierName, list[i].Qty, list[i].Amount);
                    }

                    totalamount += itotal;
                    lblTotal2.Text = totalamount.ToString();

                }
            }
            catch (SqlException ex)
            {
                MyMessageBox.ShowBox("SQL Error " + ex.Number + ":" + ex.Message);
            }

            
        }
        private void dgPurchaseList2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string id = dgPurchaseList2.CurrentRow.Cells[0].Value.ToString();
            string des = PurchaseManager.getItemDes(id);
            cboItem.Text = des;
            cbSName.Text = dgPurchaseList2.CurrentRow.Cells[1].Value.ToString();
            txtQty2.Text = dgPurchaseList2.CurrentRow.Cells[2].Value.ToString();
            errorProvider1.Clear();
        }
        private void btnProceed2_Click(object sender, EventArgs e)
        {
            List<PurchaseObj> list = new List<PurchaseObj>();
            foreach (DataGridViewRow row in dgPurchaseList2.Rows)
            {
                if (row != null)
                {
                    PurchaseObj purchase = new PurchaseObj();
                    purchase.Date = Convert.ToDateTime(lblDate.Text);
                    purchase.ItemNo = row.Cells[0].Value.ToString();
                    purchase.SupplierName = row.Cells[1].Value.ToString();
                    purchase.Qty = Convert.ToInt32(row.Cells[2].Value.ToString());
                    purchase.Amount = Convert.ToDouble(row.Cells[3].Value.ToString());
                    purchase.Total = Convert.ToDouble(lblTotal2.Text);
                    list.Add(purchase);
                }
            }
            PurchaseManager.addPurchaseList(list);
            frmPurchaseList form = new frmPurchaseList();
            form.Hide();
            frmPurchaseOrder form2 = new frmPurchaseOrder();
            form2.Show();
        }

        private void cboItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = cboItem.SelectedValue.ToString();
            Itemprice = PurchaseManager.getItemPrice(id);
            lblPrice.Text = Itemprice.ToString();
            total = Itemprice;
        }

#region Validation

        private Boolean MyValidation()
        {
            errorProvider1.Clear();
            if (txtQty.Text.Trim() == "" || DBBroker.Validation.IsNaturalNumber(txtQty.Text) == false || txtQty.Text == "0")
            {
                errorProvider1.SetError(txtQty, "Please Type Valid Quantity");
                return false;
            }

            //else if (txtQty2.Text.Trim() == "" || DBBroker.Validation.IsNaturalNumber(txtQty2.Text) == false || txtQty2.Text == "0")
            //{
            //    errorProvider1.SetError(txtQty2, "Please Type Valid Quantity");
            //    return false;
            //}
            else
            {
                return true;
            }
        }

        private Boolean MyValidate()
        {
            errorProvider1.Clear();
            

            if (txtQty2.Text.Trim() == "" || DBBroker.Validation.IsNaturalNumber(txtQty2.Text) == false || txtQty2.Text == "0")
            {
                errorProvider1.SetError(txtQty2, "Please Type Valid Quantity");
                return false;
            }
            else
            {
                return true;
            }
        }

#endregion
    }
}

